package com.ieasy.module.site.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.model.Pager;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.basic.util.dbutil.IDBUtilsHelper;
import com.ieasy.module.common.service.BaseService;
import com.ieasy.module.site.entity.ColumnEntity;
import com.ieasy.module.site.entity.DocEntity;
import com.ieasy.module.site.web.form.DocForm;

/**
 * @author 杨浩泉
 *
 */
@Service @Transactional
public class DocService extends BaseService implements IDocService {
	
	private Logger logger = Logger.getLogger(DocService.class) ;

	@Inject
	private IBaseDao<DocEntity> basedaoRole ;
	
	@Inject
	private IDBUtilsHelper dbutil ;
	
	@Override
	public Msg add(DocForm form) {
		try {
			DocEntity entity = new DocEntity() ;
			BeanUtils.copyNotNullProperties(form, entity) ;
			entity.setCreated(new Date()) ;
			entity.setCreateName(this.getCurrentUser().getEmp_name()) ;
			
			if(null != form.getPid() && !"".equals(form.getPid())) {
				ColumnEntity c = new ColumnEntity() ;
				c.setId(form.getPid()) ;
				entity.setCol(c) ;
			}
			
			this.basedaoRole.add(entity) ;
			
			return new Msg(true, "添加成功！");
		} catch (Throwable e) {
			logger.error("添加角色信息失败===>异常信息：", e) ;
			return new Msg(false, "添加失败！") ;
		}
	}

	@Override
	public Msg delete(DocForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.basedaoRole.delete(DocEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			}
		} catch (Exception e) {
			logger.error("根据ID["+form.getIds()+"]删除失败===>异常信息：", e) ;
			return new Msg(false, "删除失败！") ;
		}
		return null;
	}

	@Override
	public Msg update(DocForm form) {
		try {
			
			DocEntity entity = this.basedaoRole.load(DocEntity.class, form.getId()) ;
			BeanUtils.copyNotNullProperties(form, entity) ;
			entity.setModifyDate(new Date()) ;
			entity.setModifyName(this.getCurrentUser().getEmp_name()) ;
			
			if(null != form.getPid() && !"".equals(form.getPid())) {
				ColumnEntity c = new ColumnEntity() ;
				c.setId(form.getPid()) ;
				entity.setCol(c) ;
			} else {
				entity.setCol(null) ;
			}
			
			this.basedaoRole.update(entity) ;
			return new Msg(true, "修改成功！");
		} catch (Exception e) {
			logger.error("修改角色信息失败===>异常信息：", e) ;
			return new Msg(false, "修改角色信息失败！") ;
		}
	}
	
	@Override
	public DocForm get(DocForm form) {
		try {
			DocEntity entity = this.basedaoRole.load(DocEntity.class, form.getId()) ;
			BeanUtils.copyNotNullProperties(entity, form) ;
			
			return form ;
		} catch (Exception e) {
			logger.error("加载角色信息失败===>异常信息：", e) ;
		}
		return null ;
	}

	@Override
	public DataGrid datagrid(DocForm form) {
		try {
			List<DocForm> forms = new ArrayList<DocForm>() ;
			Pager<DocForm> pager = this.find(form) ;
			if (null != pager && !pager.getDataRows().isEmpty()) {
				for(DocForm pf : pager.getDataRows()) {
					forms.add(pf) ;
				}
			}
			DataGrid dg = new DataGrid() ;
			dg.setTotal(pager.getTotal());
			dg.setRows(forms);
			return dg;
		} catch (Exception e) {
			logger.error("加载角色列表信息失败===>异常信息：", e) ;
		}
		return null;
	}
	
	private Pager<DocForm> find(DocForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String sql = "select t.* from ieasy_site_doc t where 1=1 ";
		sql = addWhere(sql, form, alias) ;
		
		return this.basedaoRole.findSQL(sql, alias, DocForm.class, false) ;
	}
	
	private String addWhere(String sql, DocForm form, Map<String, Object> params) {
		if (null != form) {
			if (form.getPageTitle() != null && !"".equals(form.getPageTitle().trim())) {
				sql += " and t.pageTitle like :pageTitle";
				params.put("pageTitle", "%%" + form.getPageTitle() + "%%");
			}
		}
		return sql;
	}
	
	

}
